import requests
import pandas as pd
import os
import re
import json


class video(object):
    def spider(self, user_id):
        url = "https://www.ixigua.com/api/videov2/author/new_video_list?"
        headers = {
            "cookie": "__ac_nonce=061163d3c0084c5968ea3;",
            "referer": f"https://www.ixigua.com/home/{user_id}/",
            "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; NEM-AL10 Build/HONORNEM-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/043906 Mobile Safari/537.36 MicroMessenger/6.6.1.1220(0x26060133) NetType/WIFI Language/zh_CN",
        }
        params = {"to_user_id": user_id, "offset": "0", "limit": "230"}  # 起始数字  # 最大数字
        response = requests.get(url=url, headers=headers, params=params).json()

        print(response)
        author = response["data"]["videoList"][0]["media_name"]
        video_list = response["data"]["videoList"]
        print(f"账号  {author}  一共有{len(video_list)}个视频")

        fp = open(f"./{author}.json", "w", encoding="utf8")
        json.dump(response, fp=fp, ensure_ascii=False, indent=True)

        return video_list

    def make_dict(self, video_list):
        title = re.findall(r"'title': '(.*?)'", str(video_list))
        video_id = re.findall(r"'item_id': '(.*?)'", str(video_list))

        print(f"标题有{len(title)}个,视频id有 {len(video_id)}个")  # 此处不一样程序会报错， 不一样的原因未知
        video_dict = {video_id[i]: title[i] for i in range(len(video_id))}
        # print(video_dict)
        return video_id, video_dict

    def Tocsv(self, video_list):
        if not os.path.exists("./video"):
            os.mkdir("./video")
        # print(video_list)
        title = re.findall(r"'title': '(.*?)'", str(video_list))
        video_id = re.findall(r"'item_id': '(.*?)'", str(video_list))
        video_url = ["https://www.ixigua.com/" + i for i in video_id]
        print(f"标题有{len(title)}个,视频id有 {len(video_id)}个")  # 此处不一样程序会报错， 不一样的原因未知
        DF = pd.DataFrame({"标题": title, "视频id": video_id, "视频网址": video_url})
        print(DF)
        path = f"./video/1.csv"
        DF.to_csv(path, index=0)

    def run(self, user_id):
        video_list = self.spider(user_id)

        # make_dict 和Tocsv 相互独立
        video_id, video_dict = self.make_dict(video_list)
        self.Tocsv(video_list)
        return video_id, video_dict


if __name__ == "__main__":
    # 2317401847250772     3658765248505175
    user_id = "3658765248505175"
    mama = video()
    mama.run(user_id)
